package io.renren.modules.eightapp.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.eightapp.dto.EiCostInfoDTO;
import io.renren.modules.eightapp.entity.EiCostInfoEntity;
import io.renren.modules.eightapp.excel.EiCostInfoExcel;
import io.renren.modules.eightapp.service.EiCostInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author Mark 1132115960@gmail.com
 * @since 1.0.0 2022-12-27
 */
@RestController
@RequestMapping("eightapp/eicostinfo")
@Api(tags = "")
public class EiCostInfoController {
    @Autowired
    private EiCostInfoService eiCostInfoService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String")
    })
    @RequiresPermissions("eightapp:eicostinfo:page")
    public Result<PageData<EiCostInfoDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<EiCostInfoDTO> page = eiCostInfoService.page(params);

        return new Result<PageData<EiCostInfoDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("eightapp:eicostinfo:info")
    public Result<EiCostInfoDTO> get(@PathVariable("id") Long id) {
        EiCostInfoDTO data = eiCostInfoService.get(id);

        return new Result<EiCostInfoDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("eightapp:eicostinfo:save")
    public Result save(@RequestBody EiCostInfoDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        Date date = new Date();
        dto.setCreateTime(date);
        dto.setUpdateTime(date);
        eiCostInfoService.save(dto);
        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("eightapp:eicostinfo:update")
    public Result update(@RequestBody EiCostInfoDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        Date date = new Date();
        dto.setUpdateTime(date);
        eiCostInfoService.update(dto);
        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("eightapp:eicostinfo:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        eiCostInfoService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("eightapp:eicostinfo:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<EiCostInfoDTO> list = eiCostInfoService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, EiCostInfoExcel.class);
    }

    @GetMapping("confirmPayment/{id}")
    @ApiOperation("确认支付")
    @LogOperation("确认支付")
    public Result confirmPayment(@PathVariable("id") Long id) {
        eiCostInfoService.confirmPayment(id);
        return new Result();
    }

    @GetMapping("getCostInfoAllDataList")
    @ApiOperation("CostInfo表，获取所有订单")
    @LogOperation("CostInfo表，获取所有订单")
    public Result<List<EiCostInfoDTO>> getCostInfoAllDataList() {
        List<EiCostInfoDTO> allDataList = eiCostInfoService.getCostInfoAllDataList();
        return new Result<List<EiCostInfoDTO>>().ok(allDataList);
    }

    @GetMapping("getCostInfoDataListByOwnerId/{ownerId}")
    @ApiOperation("CostInfo表，根据业主ID获取订单")
    @LogOperation("CostInfo表，根据业主ID获取订单")
    public Result<List<EiCostInfoDTO>> getCostInfoDataListByOwnerId(@PathVariable("ownerId") Integer ownerId) {
        List<EiCostInfoDTO> listByOwnerId = eiCostInfoService.getCostInfoDataListByOwnerId(ownerId);
        return new Result<List<EiCostInfoDTO>>().ok(listByOwnerId);
    }

    @GetMapping("getCostInfoDataListByType")
    @ApiOperation("CostInfo表，根据费用类型获取订单")
    @LogOperation("CostInfo表，根据费用类型获取订单")
    public Result<List<EiCostInfoDTO>> getCostInfoDataListByType(@Param("type") String type){
        List<EiCostInfoDTO> listByType = eiCostInfoService.getCostInfoDataListByType(type);
        return new Result<List<EiCostInfoDTO>>().ok(listByType);
    }
}